package homework.test8;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

/*
使用字节输出流将集合ArrayList<Student>中存储的学生对象 写到文件中去
 */
public class Demo8 {
    public static void main(String[] args) throws IOException {
        ArrayList<Student> list = new ArrayList<>();
        Student stu1 = new Student("张三",23);
        Student stu2 = new Student("李四",22);
        Student stu3 = new Student("王五",21);
        Student stu4 = new Student("赵六",23);

        Collections.addAll(list,stu1,stu2,stu3,stu4);

        FileOutputStream fos = new FileOutputStream("Day11\\src\\homework\\test8\\a.txt");
        fos.write(list.toString().getBytes());

        fos.close();

        FileOutputStream fos2 = new FileOutputStream("Day11\\src\\homework\\test8\\b.txt");
        for (Student stu : list) {
            fos2.write(stu.toString().getBytes());
            fos2.write("\r\n".getBytes());
        }

        fos2.close();
    }
}
